<template>
  <div>
    <el-dialog :visible.sync='isShow' title='绑定机构' width="800px" @open="openDialogInit" @close="resetForm" :close-on-click-modal=false>
      <div v-loading="loading">
        <el-card>
          <el-form :model="formInline" ref="formInline" size="mini" label-position="left" label-width="125px">
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="机构组编号:" prop="branchgroupnum">
                  <el-input v-model="formInline.branchgroupnum" disabled></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="机构组名称:" prop="branchgroupname">
                  <el-input v-model="formInline.branchgroupname" disabled></el-input>
                </el-form-item>
              </el-col>

              <el-col :span="12">
                <el-form-item label="法人号:" prop="LegalPersonNum">
                  <el-input v-model="formInline.legalpersonnum" disabled></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="法人名称:" prop="LegalPersonName">
                  <el-input v-model="formInline.LegalPersonName" disabled></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-card>
        <el-card style="margin-top:10px;max-height:300px;overflow:auto;">
          <el-tree :data="pageList" @node-contextmenu="rightClick" ref="tree" :props="props" show-checkbox :default-checked-keys="pageList1" node-key="branchnum" @check-change="handleCheckChange">
          </el-tree>
        </el-card>
        <el-row style="margin:20px 0;text-align:right">
          <el-button size="mini" @click="isShow=false">取消</el-button>
          <el-button type="primary" size="mini" @click="branchGroupBranch">确认</el-button>
        </el-row>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import request from "@/common/utils/request";
import branchGroup from "@/message/branchManage/branchGroupMgr/branch-group-mgr";
import branchGroupBranch from "@/message/branchManage/branchGroupMgr/branch-group-branch";
import toolBox from "@/common/utils/toolBox";
export default {
  props: { option: Object },
  data() {
    return {
      loading: false,
      formInline: {},
      pageList: [],
      pageList1: [],
      PostList: [],
      props: {
        label: "branchname",
        children: "children"
      }
    };
  },
  computed: {
    isShow: {
      get: function() {
        return this.option.isShow;
      },
      set: function(newValue) {
        this.option.isShow = newValue;
      }
    }
  },
  methods: {
    rightClick(event,data,Node,slfe){
      console.log(event,data,Node,slfe)
    },
    // 打开窗口初始化
    openDialogInit() {
      this.formInline = this.option.currentRow;
      this.formInline.legalpersonnum = this.$store.getters.LegalPersonNum;
      this.formInline.LegalPersonName = this.$store.getters.LegalPersonName;
      this.branchGroupBranchQuery();
    },

    // 机构组机构查询
    branchGroupBranchQuery() {
      let resBody = new branchGroupBranch();
      resBody.data.BranchGroupNum = this.formInline.branchgroupnum;
      resBody.data.LegalPersonNum = this.formInline.legalpersonnum;
      resBody.data.OperType = "Q"; // 操作类型
      request(resBody)
        .then(response => {
          if (response.SYS_HEAD.ReturnCode === "000000") {
            this.pageList = response.RspInfo.pageList || [];
            this.pageList = toolBox.formatTreeData(
              this.pageList,
              "branchnum",
              "supebranchnum"
            );
            this.pageList1 = response.RspInfo.pageList1 || [];
            let temp = [];
            for (let i = 0; i < this.pageList1.length; i++) {
              const element = this.pageList1[i].branchnum;
              temp.push(element);
            }
            this.pageList1 = temp;
          } else {
            this.$message({
              type: "error",
              message: response.SYS_HEAD.ReturnMessage
            });
          }
        })
        .catch(error => {
          console.error("查询错误", error);
        });
    },

    // 树值改变事件
    handleCheckChange(data, checked, indeterminate) {
      let keyList = this.$refs.tree.getCheckedKeys();
      this.PostList = [];
      for (let i = 0; i < keyList.length; i++) {
        const element = keyList[i];
        let PostList = {
          BranchNum: element
        };
        this.PostList.push(PostList);
      }
    },

    // 重置表单
    resetForm() {
      this.$refs["formInline"].resetFields();
    },

    /**
     * @desc 机构组机构维护
     *
     */
    branchGroupBranch() {
      this.$refs["formInline"].validate(valid => {
        if (valid) {
          this.loading = true;
          let resBody = new branchGroupBranch();
          resBody.data.BranchGroupNum = this.formInline.branchgroupnum;
          resBody.data.LegalPersonNum = this.formInline.legalpersonnum;
          resBody.data.PostList = this.PostList;
          resBody.data.OperType = "M"; // 操作类型
          request(resBody)
            .then(response => {
              if (response.SYS_HEAD.ReturnCode === "000000") {
                this.loading = false;
                this.$message({
                  type: "success",
                  message: "绑定成功!"
                });
                this.isShow = false;
                this.$emit("refreshBrand");
              } else {
                this.loading = false;
                this.$message({
                  type: "error",
                  message: response.SYS_HEAD.ReturnMessage
                });
              }
            })
            .catch(error => {
              this.loading = false;
              console.log(error);
            });
        } else {
          this.$message({
            type: "warning",
            message: "必输项不能为空"
          });
          return false;
        }
      });
    }
  }
};
</script>
<style lang="sass" scoped>

</style>
